package com.techfort.testguice.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.techfort.testguice.pojo.Favourite;


public class FavouriteDAO implements Crud<Favourite> {
		private static final String DATABASE_TABLE = "favourites";
		private static final String TITLE = "title";
		
		private final Context context;
		private static final String TAG = "INFO";
		private static final String CREATE = 
			"create table favourites (title text, unique(title))";
		
		private DatabaseHelper dh;
		private SQLiteDatabase db;
		
		
		
		public FavouriteDAO(Context ctx){
			this.context = ctx;
			dh = new DatabaseHelper(context);
			db = dh.getWritableDatabase();
		}
		@Override
		public void delete(Favourite object) {
			delete(object.getTitle());
		}

		@Override
		public Favourite get(String title) {
			Cursor cur = getTitle(title);
			return new Favourite(cur.getString(1));
		}

		@Override
		public List<Favourite> getAll() {
			List<Favourite> bms = new ArrayList<Favourite>();
			
			Cursor cur = getAllTitles();
			while(cur.isAfterLast()==false){
				bms.add(new Favourite(cur.getString(1)));
				cur.moveToNext();
			}
			return bms;
		}
		@Override
		public void store(Favourite object) {
			insertTitle(object.getTitle() );
		}
		
		
	    private static class DatabaseHelper extends SQLiteOpenHelper 
	    {
	        DatabaseHelper(Context context) 
	        {
	            super(context, DATABASE_NAME, null, DATABASE_VERSION);
	        }

	        @Override
	        public void onCreate(SQLiteDatabase db) 
	        {
	            db.execSQL(CREATE);
	        }

	        @Override
	        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
	                              int newVersion) 
	        {
	            Log.w(TAG, "Upgrading database from version " + oldVersion 
	                  + " to "
	                  + newVersion + ", which will destroy all old data");
	            db.execSQL("DROP TABLE IF EXISTS titles");
	            onCreate(db);
	        }
	    }   
	    
	    
	    public FavouriteDAO open() throws SQLException 
	    {
	        db = dh.getWritableDatabase();
	        return this;
	    }

	    //---closes the database---    
	    public void close() 
	    {
	        db.close();
	    }
	    
	    //---insert a title into the database---
	    public long insertTitle(String title) 
	    {
	        ContentValues initialValues = new ContentValues();
	        
	        initialValues.put(TITLE, title);
	        return db.insert(DATABASE_TABLE, null, initialValues);
	    }

	    //---deletes a particular title---
	    public boolean delete(String title) 
	    {
	        return db.delete(DATABASE_TABLE, TITLE + 
	        		"=" + title, null) > 0;
	    }

	    //---retrieves all the titles---
	    public Cursor getAllTitles() 
	    {
	        return db.query(DATABASE_TABLE, new String[] { TITLE}, 
	                null, 
	                null, 
	                null, 
	                null, 
	                null);
	    }

	    //---retrieves a particular title---
	    public Cursor getTitle(String title) throws SQLException 
	    {
	        Cursor mCursor =
	                db.query(true, DATABASE_TABLE, new String[] {
	                		TITLE
	                		}, 
	                		TITLE + "=" + title, 
	                		null,
	                		null, 
	                		null, 
	                		null, 
	                		null);
	        if (mCursor != null) {
	            mCursor.moveToFirst();
	        }
	        return mCursor;
	    }

	    //---updates a title---
	    public boolean updateTitle(String title) 
	    {
	        ContentValues args = new ContentValues();
	        args.put(TITLE, title);
	        
	        return db.update(DATABASE_TABLE, args, 
	                         TITLE + "=" + title, null) > 0;
	    }
		


}
